<?php


namespace App\Http\Controllers;


use App\Models\Menu;
use App\Service\ApiResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class MenusController
{
    public function list(Request $request)
    {
        $userAgent = $request->userAgent();
        $isMobile = strpos(strtolower($userAgent), 'mobile') !== false;
        $params = $_POST;
        $categoryID = $params['category_id'];
        $page = empty($params['page']) ? 1 : $params['page'];

        $model = new Menu();
        $where = [
            'state' => 1,
            'is_delete' => 0,
            'category_id' => $categoryID
        ];
        $count = $model::where($where)->where('issue_time', '<=', date('Y-m-d H:i:s'))->count();
        $totalPage = ceil($count / 20);
        $menuList = $model::select(['id', 'content_cover as image','cover', 'name'])
            ->where($where)
            ->where('issue_time', '<=', date('Y-m-d H:i:s'))
            ->orderBy('orid', 'asc')
            ->orderBy('id', 'desc')
            ->limit(20)
            ->offset(20 * ($page - 1))
            ->get();
        foreach ($menuList as $key => $val) {
            $menuList[$key]['image'] = $model->getContentCoverAttribute($val['cover']);
        }
        $result = [
            'total_page' => $totalPage,
            'total_count' => $count,
            'list' => $menuList
        ];
        return ApiResponse::success($result);
    }

    public function detail()
    {
        $params = $_POST;
        $id = $params['id'];
        $model = new Menu();
        $where = [
            'id' => $id,
            'is_delete' => 0,
        ];
        $menuDetail = $model::select([
            'id', 'name', 'cover', 'content_cover','category_id', 'smalltitle as title', 'video', 'video_cover', 'ingredient', 'method'
        ])
            ->where($where)
            ->first();

        $menuDetail->cover = empty($menuDetail->cover) ? '' : $model->getContentCoverAttribute($menuDetail->cover);
        $menuDetail->content_cover = empty($menuDetail->content_cover) ? '' : $model->getContentCoverAttribute($menuDetail->content_cover);
        $menuDetail->video = empty($menuDetail->video) ? '' : $model->getContentCoverAttribute($menuDetail->video);
        $menuDetail->video_cover = empty($menuDetail->video_cover) ? $model->getContentCoverAttribute($menuDetail->content_cover) : $model->getVideoCoverAttribute($menuDetail->video_cover);
        if (!empty($menuDetail->ingredient)) {
            $item = json_decode($menuDetail->ingredient, true);
            $formattedProducts = [];
            foreach ($item as $v) {
                $formattedProducts[] = [
                    'name' => $v['用料'],
                    'count' => $v['单位']
                ];
            }
            $menuDetail->ingredient = $formattedProducts;
        } else {
            $menuDetail->ingredient = [];
        }
        if (!empty($menuDetail->method)) {
            $item = json_decode($menuDetail->method, true);
            $formattedProducts = [];
            foreach ($item as $v) {
                $formattedProducts[] = [
                    'content' => $v['做法'],
                    'image' => empty($v['图片']) ? '' : $model->getCoverAttribute($v['图片'])
                ];
            }
            $menuDetail->method = $formattedProducts;
        } else {
            $menuDetail->method = [];
        }

        $where = [
            'state' => 1,
            'is_delete' => 0,
            'isrecommend' => 1
        ];
        $recommendList = $model::select(['id', 'content_cover as image', 'name'])
            ->where($where)
            ->where('issue_time', '<=', date('Y-m-d H:i:s'))
            ->orderBy('orid', 'asc')
            ->orderBy('id', 'desc')
            ->get();
        foreach($recommendList as $key => $val)
        {
            $recommendList[$key]['image'] = $model->getContentCoverAttribute($val['image']);
        }
        $menuDetail->recommend = $recommendList;
        return ApiResponse::success($menuDetail);
    }
}
